using System;

namespace Scenario.Modules.DM.DataModel
{
	public class XMLDocuments : bOSdotNET.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE	= "Documents";

		private bool bDocumentTemplate;

		public XMLDocuments(bool bTemplate)
		{
			this.Init();

			bDocumentTemplate = bTemplate;
		}

		public XMLDocuments(System.Xml.XmlNode nodeList, bool bTemplate)
		{
			this.Parse (nodeList);

			bDocumentTemplate = bTemplate;
		}

		public override void Init()
		{
			this.RemoveAll();
		}

		public override void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLDocumentBase.MAIN_NODE:

						if ( bDocumentTemplate )
							this.Add ( new XMLDocumentTemplate(node));
						else
							this.Add ( new XMLDocumentInstance(node));

						break;
				}
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLDocuments.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		public XMLDocumentBase Get (string sDocumentName)
		{
			XMLDocumentBase document;

			for ( int i=0; i<this.List.Count;i++)
			{
				document = (XMLDocumentBase)this[i];

				if ( document.Name == sDocumentName )
				{
					return document;
				}
			}

            throw new Scenario.Modules.DM.DataModel.Exception.DocumentNotFound(sDocumentName);
		}
	}
}